package datasource.image;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.StringTokenizer;

import com.aliasi.tokenizer.PorterStemmerTokenizerFactory;

public class EventClassifier {
	private HashMap<String, HashSet<String>> events = new HashMap<String, HashSet<String>>();

	public EventClassifier(String conceptFile) {
		try {
			BufferedReader br = new BufferedReader(new FileReader(conceptFile));
			String line = null;
			while ((line = br.readLine()) != null) {
				StringTokenizer st = new StringTokenizer(line, "|");
				String eventName = st.nextToken();
				StringTokenizer tagSt = new StringTokenizer(st.nextToken(), ",");
				HashSet<String> set = new HashSet<String>();
				while (tagSt.hasMoreTokens()) {
					String stemmedWord = PorterStemmerTokenizerFactory
							.stem(tagSt.nextToken());
					set.add(stemmedWord);
				}
				events.put(eventName, set);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public HashSet<String> classifyImage(ImageObj obj) {
		HashSet<String> eventTags = new HashSet<String>();
		for (String key : events.keySet()) {
			HashSet<String> tags = events.get(key);
			double prob = 1;
			for (String tag : obj.tags) {
				String stemmedWord = PorterStemmerTokenizerFactory.stem(tag);
				if (tags.contains(stemmedWord)) {
					prob *= 0.2;
				}
			}
			prob = 1 - prob;
			if (prob > 0) {
				obj.addEventTag(key, prob);
			}

		}
		return eventTags;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
